package com.gisuni.sddc.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
	public static String str2MD5(String str) {
		String ret = "";

		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			try {
				md.update(str.getBytes("utf-8"));
			} catch (UnsupportedEncodingException e) {
				md.update(str.getBytes());
			}

			//md.update(str.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}

			ret = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	private void mian() {
		MD5Util.str2MD5("1");

	}
	
	public static void main(String[] args) {
	  System.out.println(MD5Util.str2MD5("123"));
	}
}
